package com.by_man.presenter.module_health_records.upload_health_records;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.by_man.presenter.R;
import com.by_man.presenter.SharedApplication;
import com.by_man.presenter.custom_layout.ViewPagerFixed;
import com.by_man.presenter.module_gestureLock.GestureLockActivity;
import com.by_man.presenter.module_health_records.image.BitmapManager;

import java.io.IOException;
import java.util.ArrayList;

import uk.co.senab.photoview.PhotoView;

/**
 * Created by man on 2017-03-05.
 */
public class PhotoGalleryActivity extends AppCompatActivity {
    private ViewHolder views;
    private ArrayList<View> listViews = null;
    private MyPageAdapter adapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_gallery);
        initView();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = new Intent(this, UploadHealthRecordsActivity.class);
            startActivity(intent);
        }
        return super.onKeyDown(keyCode, event);
    }

    protected void onRestart()
    {
        super.onRestart();
        Intent intent=new Intent(this,GestureLockActivity.class);
        intent.putExtra("class",this.getClass().getName());
        intent.putExtra("position", 0);
        startActivity(intent);
    }

    private void initView(){
        views = new ViewHolder();
        views.back_bt = (Button) findViewById(R.id.gallery_back);
        views.send_bt = (Button) findViewById(R.id.send_button);
        views.del_bt = (Button) findViewById(R.id.gallery_del);
        views.pager = (ViewPagerFixed) findViewById(R.id.gallery01);
        showOkBtn();
        views.send_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(PhotoGalleryActivity.this, UploadHealthRecordsActivity.class);
                startActivity(intent);
            }
        });
        views.del_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (1 == listViews.size()) {
                    BitmapManager.images.clear();
                    Intent intent = new Intent(PhotoGalleryActivity.this, ChosePhotoActivity.class);
                    startActivity(intent);
                } else {
                    int position = views.pager.getCurrentItem();
                    BitmapManager.images.remove(position);
                    views.pager.removeAllViews();
                    listViews.remove(position);
                    views.send_bt.setText("ok"+"(" + BitmapManager.images.size() + "/"+BitmapManager.max+")");
                    adapter.setViewArrayList(listViews);
                    adapter.notifyDataSetChanged();
                }
            }
        });
        views.back_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(PhotoGalleryActivity.this, AlbumActivity.class);
                startActivity(intent);
            }
        });
        initViewList();
        adapter = new MyPageAdapter();
        adapter.setViewArrayList(listViews);
        views.pager.setPageMargin(10);
        views.pager.setAdapter(adapter);
    }

    private void showOkBtn() {
        if (BitmapManager.images.size() > 0) {
            views.send_bt.setText("ok"+"(" + BitmapManager.images.size() + "/"+BitmapManager.max+")");
            views.send_bt.setPressed(true);
            views.send_bt.setClickable(true);
            views.send_bt.setTextColor(Color.WHITE);
        } else {
            views.send_bt.setPressed(false);
            views.send_bt.setClickable(false);
            views.send_bt.setTextColor(Color.parseColor("#E1E0DE"));
        }

    }

    private void initViewList(){
        if (null == listViews)
            listViews = new ArrayList<View>();
        String path;
        for (int i = 0; i < BitmapManager.images.size(); i++) {
            path = BitmapManager.images.get(i).imagePath;
            final PhotoView img = new PhotoView(this);
            img.setBackgroundColor(0xff000000);
            img.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            // 异步加载任务
            AsyncTask<String, Void, Bitmap> loader = new AsyncTask<String, Void, Bitmap>() {
                @Override
                protected Bitmap doInBackground(String... params) {
                    Bitmap bitmap = null;
                    try {
                        bitmap = BitmapManager.getLocalBitmap(params[0]);
                    } catch (IOException ie) {
                        ie.printStackTrace();
                    }

                    return bitmap;
                }

                @Override
                protected void onPostExecute(Bitmap bitmap) {
                    if (bitmap != null)
                        img.setImageBitmap(bitmap);
                }
            };
            loader.execute(path);
            listViews.add(img);
        }
    }

    private class ViewHolder{
        Button back_bt;
        Button send_bt;
        Button del_bt;
        ViewPagerFixed pager;
    }

    private class MyPageAdapter extends PagerAdapter {
        private ArrayList<View> viewArrayList;
        @Override
        public boolean isViewFromObject(View view, Object o) {
            return view == o;
        }

        @Override
        public int getCount(){
            return viewArrayList.size();
        }

        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        public void destroyItem(View view, int position, Object o){
            ((ViewPagerFixed) view).removeView(viewArrayList.get(position % viewArrayList.size()));
        }

        public void finishUpdate(View arg0) {
        }

        public Object instantiateItem(View view, int position){
            //try {
                ((ViewPagerFixed) view).addView(viewArrayList.get(position % viewArrayList.size()), 0);
           // } catch ()
            return viewArrayList.get(position % viewArrayList.size());
        }

        public void setViewArrayList(ArrayList<View> lists) {
            viewArrayList = lists;
        }

    }

}
